# 380 中等：O(1)时间插入、删除和获取随机元素
import random


class RandomizedSet:
    def __init__(self):
        self.arrs = []
        self.index = {}

    def insert(self, val: int) -> bool:
        if val in self.index:
            return False

        self.index[val] = len(self.arrs)
        self.arrs.append(val)
        return True

    def remove(self, val: int) -> bool:
        if val not in self.index:
            return False
        i = self.index[val]
        self.arrs[i] = self.arrs[-1]
        self.index[self.arrs[i]] = i
        self.arrs.pop()
        del self.index[val]
        return True

    def getRandom(self) -> int:
        # return self.arrs[random.randint(0, len(self.arrs) - 1)]
        return random.choice(self.arrs)


# Your RandomizedSet object will be instantiated and called as such:
obj = RandomizedSet()
param_1 = obj.insert(1)
param_4 = obj.insert(3)
param_5 = obj.insert(2)
param_2 = obj.remove(2)
param_3 = obj.getRandom()
print(obj, param_1, param_4, param_2, param_3)
